<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager"%>

<%
    String chipTracContext      = "/" + ConfigManager.getChipTracContext();
    String closeWindowAfterSave = ( String ) request.getAttribute( "closeWindow" );
%>
<HTML>
<HEAD>
<TITLE>ScalarSoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type=text/css rel=stylesheet>

<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<SCRIPT src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>

<SCRIPT language="JavaScript">

<% if ( ( closeWindowAfterSave != null ) && ( closeWindowAfterSave.equalsIgnoreCase( "true" ) ) ) { %>
	window.close();
<% } %>

var customerId = <%= request.getParameter("customerId")%>;
function validateData(theForm)
{
    var pa = document.getElementById("errors");
    pa.innerHTML = "";
    if(trim(theForm.address1.value).length==0)
    {
        pa.innerHTML="<li>Address1 cannot be empty";
        theForm.address1.focus();
        return false;
    }
    if( (trim(theForm.phoneNumber.value).length != 0)  || (trim(theForm.faxNumber.value).length != 0 ) || (trim(theForm.cellNumber.value).length != 0) || (trim(theForm.email.value).length != 0) )
    {
        if(trim(theForm.contactPerson.value).length==0)
        {
            pa.innerHTML="<li>Contact Person cannot be empty when Phone Number or Fax Number or Cell Number or Email is Entered";
            theForm.contactPerson.focus();
            return false;
        }
    }
    /*
    if(!(theForm.address1.value!="" ||  theForm.address2.value!="" || theForm.city.value!="" || theForm.state.value!="" || theForm.zipCode.value!="" || theForm.country.value!="" ))
    {
        pa.innerHTML="Address details cannot be empty";
        theForm.address1.focus();
        return false;
    }
    if( !(is_Sp_Characters_phone(theForm.phoneNumber.value)) || (check_char_position3(theForm.phoneNumber.value)) )
    {
        pa.innerHTML="Phone No should be a numerical ";
        return false;
	}
    if( !(is_Sp_Characters_phone(theForm.faxNumber.value)) || (check_char_position3(theForm.faxNumber.value)) )
    {
        pa.innerHTML="Fax should be a numerical";
        return false;
    }
    */
    if (!isValidMailID(theForm.email.value,theForm.email,"N"))
    {
        pa.innerHTML="Enter a Valid Email ID";
        theForm.email.select();
        theForm.email.focus();
        return false;
    }
    return true;

}

function addAddress(theForm)
{


    //window.opener.document.salesOrderForm.shippingAddress.value=theForm.address1.value+"\n"+
    //theForm.address2.value+"\n"+theForm.city.value+"\n"+theForm.state.value+"\n"+theForm.zipCode.value+"\n"+theForm.country.value;

    //Added by rohan on July 28th 2003
    //Calling the method for validating form data


    if(!validateData(theForm))
        return;

    //For setting the shipping address in the shippingAddress text area  and shippingAddressID select list of sales_order.jsp page

    var strShippingAddress="";
    // Added shipping address shipAddressWithoutContactPerson by veeru on 3rd June 2004 to reset the shippingAddressMap
    var shipAddressWithoutContactPerson = "";

    var tempStateZipCountry="";
    if (theForm.address1.value!="")
    {
        strShippingAddress=strShippingAddress+theForm.address1.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.address1.value + "\n";
        window.opener.addShippingAddressIdOption(theForm.address1.value,-1);
    }
    if(theForm.contactPerson.value!="")
    {
        strShippingAddress = strShippingAddress+theForm.contactPerson.value + "\n";
        window.opener.addShippingAddressIdOption(theForm.contactPerson.value,-1);
    }    
    if(theForm.address2.value!="")
    {
        strShippingAddress=strShippingAddress + theForm.address2.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.address2.value + "\n";
        window.opener.addShippingAddressIdOption(theForm.address2.value,-1);
    }
	//Added by Sarada on 17th March 2004
	 if (theForm.address3.value!="")
    {
        strShippingAddress=strShippingAddress+theForm.address3.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.address3.value + "\n";
        window.opener.addShippingAddressIdOption(theForm.address3.value,-1);
    }
    if(theForm.address4.value!="")
    {
        strShippingAddress=strShippingAddress + theForm.address4.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.address4.value + "\n";
        window.opener.addShippingAddressIdOption(theForm.address4.value,-1);
    }
    if(theForm.city.value!="")
    {
        strShippingAddress=strShippingAddress + theForm.city.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.city.value + "\n";
        window.opener.addShippingAddressIdOption(theForm.city.value,-1);
    }
    if(theForm.state.value!="")
    {
            strShippingAddress=strShippingAddress + theForm.state.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.state.value;
            tempStateZipCountry=tempStateZipCountry + theForm.state.value;
    }
    if(theForm.zipCode.value!="")
    {
        if(theForm.state.value=="")
        {
            strShippingAddress=strShippingAddress + theForm.zipCode.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.zipCode.value;
            tempStateZipCountry=tempStateZipCountry + theForm.zipCode.value;
        }
        else
        {
            // Modified by veeru on 2nd March 2004
            strShippingAddress=strShippingAddress + " -" + theForm.zipCode.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + " -" + theForm.zipCode.value;
            tempStateZipCountry=tempStateZipCountry + " -" + theForm.zipCode.value;
        }

    }
    if(theForm.country.value!="")
    {
        if(theForm.state.value=="" && theForm.zipCode.value=="")
        {
            strShippingAddress = strShippingAddress + theForm.country.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + theForm.country.value;

            tempStateZipCountry=tempStateZipCountry + theForm.country.value;
        }
        else
        {
            strShippingAddress=strShippingAddress + " ," + theForm.country.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + " ,"  + theForm.country.value;
            tempStateZipCountry=tempStateZipCountry + " ," + theForm.country.value;
        }
    }
    if(tempStateZipCountry!="")
        window.opener.addShippingAddressIdOption(tempStateZipCountry,-1);
    if(theForm.phoneNumber.value != "")
    {
    strShippingAddress  = strShippingAddress +"\n\nTel:"+ theForm.phoneNumber.value;
    }
    if(theForm.cellNumber.value != "")
    {
    strShippingAddress  = strShippingAddress +"\nCell:"+ theForm.cellNumber.value;
    }
    if(theForm.faxNumber.value != "")
    {
    strShippingAddress  = strShippingAddress +"\nFax:"+ theForm.faxNumber.value;
    }
    if(theForm.email.value != "")
    {
    strShippingAddress  = strShippingAddress +"\nEmail:"+ theForm.email.value;
    }

    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\n\nTel:";
    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\nCell:";
    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\nFax:";
    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\nEmail:";



    // moved by veeru to the opener form on 22nd May 2004
    //window.opener.document.salesOrderForm.shippingAddress.value=strShippingAddress;
    window.opener.addShippingAddressIdOption("",-1);
    //window.opener.document.salesOrderForm.shippingAddressId.value=-1;
    // End of additions by rohan on July 28th 2003

    // Added by veeru on 6th May 2004
    window.opener.addShippingAddressContactPerson(shipAddressWithoutContactPerson ,strShippingAddress,theForm.contactPerson.value);
    theForm.action.value = "SaveShippingAddress";
    //theForm.customerId.value = window.opener.salesOrderForm.customerId.value;
    theForm.customerId.value = customerId;
    theForm.submit();
    //window.close();

}

function resetPage(theForm)
{
    theForm.reset();
}

// SharatH on July 31, 2003
function showErrorReportPage()
{
    newWindow = window.open ('<%=chipTracContext%>/reportProblem.do','reportProblem','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=750,height=400,resizable=no');
    newWindow.focus();
}
</SCRIPT>
</HEAD>

<BODY bgcolor="ffffff" marginheight="0" marginwidth="0" topmargin=0 leftmargin=0>

<%
    String closeWindowAfterSessionTimeOut = ( String ) request.getAttribute( "closeWindowAfterSessionExp" );
    if ( closeWindowAfterSessionTimeOut == null )
    {
%>
<table cellpadding="10" cellspacing="0" border="0" width="100%">
<tr bgcolor="#8C87A8">
    <td height="30" valign="top" align=center><font class="NArialL">
        <font color="#FFFFFF">Add Shipping Address for - Customer</font><br>
    </td>
    <td valign="top" align="right">
        <a href="javascript:window.close()"><font class="NArial">Close</font></a>
    </td>
</tr>
</table>

<html:form name="addressForm" action="/addAddress.do" method="post" type="com.scalar.chiptrack.sales.forms.AddressForm" focus="address1">

<center><span class="NArialW">Customer Shipping Address </span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10>
<b><FONT id="errors" color="FF0000" styleClass="NArialL"></FONT></b>
<br>
<b><font class="NArialL"><chiptrack:errors styleClass="NArialL"/></font></b>    
<br>
<TABLE border="0" width="100%" bordercolor="#FFFFFF">
 <TR>
       <TD width='40%' class="NArialL"> Contact Person </TD>
       <TD width='40%'><html:text property="contactPerson" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Address 1 <font color="red">*</font></TD>
       <TD width='40%'><html:text property="address1" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Address 2 </TD>
       <TD width='40%'><html:text property="address2" styleClass="NArialL" maxlength="50"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> Address 3 </TD>
       <TD width='40%'><html:text property="address3" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Address 4 </TD>
       <TD width='40%'><html:text property="address4" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> City </TD>
       <TD width='40%'><html:text property="city" styleClass="NArialL" maxlength="<%=ConfigManager.getProperty(ConfigManager.ADDRESS_CITY_MAX_LENGTH)%>"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> State </TD>
       <TD width='40%'><html:text property="state" styleClass="NArialL" maxlength="<%=ConfigManager.getProperty(ConfigManager.ADDRESS_STATE_MAX_LENGTH)%>"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Zip Code </TD>
       <TD width='40%'><html:text property="zipCode" styleClass="NArialL" maxlength="<%=ConfigManager.getProperty(ConfigManager.ADDRESS_ZIP_MAX_LENGTH)%>"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> Country</TD>
       <TD width='40%'><html:text property="country" styleClass="NArialL" maxlength="50"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> Phone Number</TD>
       <TD width='40%'><html:text property="phoneNumber" styleClass="NArialL" maxlength="<%=ConfigManager.getProperty(ConfigManager.ADDRESS_PHONE_MAX_LENGTH)%>" onkeypress='validatePhoneNumber(this)'/>
   </TR>
    <TR><!-- Added by Sarada on 8th March 2004  -->
       <TD width='40%' class="NArialL"> Cell No</TD>
       <TD width='40%'><html:text property="cellNumber" styleClass="NArialL" maxlength="50" onkeypress='validatePhoneNumber(this)'/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Fax Number</TD>
       <TD width='40%'><html:text property="faxNumber" styleClass="NArialL" maxlength="<%=ConfigManager.getProperty(ConfigManager.ADDRESS_FAX_MAX_LENGTH)%>" onkeypress='validatePhoneNumber(this)'/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Email </TD>
       <TD width='40%'><html:text property="email" styleClass="NArialL" maxlength="50"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> <bean:message key="label.address.tagName"/> </TD>
       <TD width='40%'><html:text property="tagName" styleClass="NArialL" maxlength="<%=ConfigManager.getProperty(ConfigManager.ADDRESS_TAGNAME_MAX_LENGTH)%>"/>
   </TR>
  
   <TR><TD colspan=2><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></TD></TR>
   <TR><TD colspan=2 align="center"><a href="javascript:addAddress(document.addressForm);"><img src="<%=chipTracContext%>/jsp/includes/images/save.gif" border=0></a>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="javascript:resetPage(document.addressForm);"><img src="<%=chipTracContext%>/jsp/includes/images/reset.gif" border=0></a>
       </TD>
   </TR>
</table>

<input type="hidden" name="action" value="">
<input type="hidden" name="customerId" value="">
</html:form>
<% } else { %>
    <table cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <tr>
            <td>
                <table align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <tr><td>&nbsp;</td></tr>
                    <tr><td align="center" ><font class="NArialL" ><font color="red">Session has been time out , Please close this window and login again.</font></font></td></tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr><td align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></td></tr>
                </table>
            </td>
       </tr>
       <tr><td>&nbsp;</td></tr>
    </table>
<% } %>
</BODY>
</HTML>
<%--<% }catch( Exception e ){e.printStackTrace();} %>--%>
